package com.mop.dinnerbook.dao;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/*
 数据库操作基类
 */
public class BaseDAO {

	private static String url ;//链接地址
	private static String username ;//数据库用户名
	private static String password ;//数据库密码
        
        private static boolean loaded = false;//数据库配置是否已经加载
        
        public static String config ;//数据库配置文件路径

	static {
		try {
			Class.forName("org.gjt.mm.mysql.Driver");//加载数据库驱动类
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
        /*
         加载数据库配置文件
         */
        public void loadConfig() throws IOException{
            Properties prop = new Properties();
            System.out.println(config);
            prop.load(new FileInputStream(config));
            username = prop.getProperty("username");
            password = prop.getProperty("password");
            url = prop.getProperty("url");
        }
        
	public Connection getConnection() {
		Connection conn = null;
		try {
                        if(!loaded){
                            loadConfig();//加载数据库配置文件
                            loaded = true;
                        }
			conn = DriverManager.getConnection(url, username, password);
			conn.setAutoCommit(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}
/*
    执行返回多行数据集的数据库查询操作
 */
	public List executeSelectList(String sql, Processor processor)
			throws SQLException {
		List result = new ArrayList();
		Connection conn = null;
		Statement stmt = null;
		ResultSet rst = null;
		try {
			conn = getConnection();
			stmt = conn.createStatement();
			stmt.execute(sql);
			rst = stmt.getResultSet();
			while (rst.next()) {
                                Object o = processor.product(rst);
                                if( o != null)
				result.add(o);
			}
		} finally {
			if (rst != null)
				rst.close();
			if (stmt != null)
				stmt.close();
			if (conn != null)
				conn.close();
		}
		return result;
	}
/*
  执行返回单行数据集的数据库查询操作
 */
	public Object executeSelectObject(String sql, Processor processor)
			throws SQLException {
		Object result = null;
		Connection conn = null;
		Statement stmt = null;
		ResultSet rst = null;
		try {
			conn = getConnection();
			stmt = conn.createStatement();
			stmt.execute(sql);
			rst = stmt.getResultSet();
			while (rst.next()) {
				result = processor.product(rst);
				break;
			}
		} finally {
			if (rst != null)
				rst.close();
			if (stmt != null)
				stmt.close();
			if (conn != null)
				conn.close();
		}
		return result;
	}
/*
    数据库的更新操作
 */
	public int executeUpdate(String sql) throws SQLException {
		Connection conn = null;
		Statement stmt = null;
		int rows = 0;
		try {
			conn = getConnection();
			stmt = conn.createStatement();
			rows = stmt.executeUpdate(sql);
		} finally {
			if (stmt != null)
				stmt.close();
			if (conn != null)
				conn.close();
		}
		return rows;
	}
/*
 数据库的插入操作
 */
	public int executeInsert(String sql) throws SQLException {
		Connection conn = null;
		Statement stmt = null;
		int rows = 0;
		try {
			conn = getConnection();
			stmt = conn.createStatement();
			rows = stmt.executeUpdate(sql);
		} finally {
			if (stmt != null)
				stmt.close();
			if (conn != null)
				conn.close();
		}
		return rows;
	}
/*
 数据库的删除操作
 */ 
        public int executeDelete(String sql) throws SQLException {
		Connection conn = null;
		Statement stmt = null;
		int rows = 0;
		try {
			conn = getConnection();
			stmt = conn.createStatement();
			rows = stmt.executeUpdate(sql);
		} finally {
			if (stmt != null)
				stmt.close();
			if (conn != null)
				conn.close();
		}
		return rows;
	}
}
